/**
 * UserPanel.java
 *
 * Created on 25-mar-2010, 16.12.55
 * 
 * @author Beatrice Bacelli
 * @author Luca Bedogni
 * @author Silvia Righini
 */
package unibo.lsb.graphic;

import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.JTextPane;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

/**
 * This class represents the middle panel in the main game table window (
 * {@link GameTableWindow}), and holds the text messages.
 */
public class InformationPanel extends javax.swing.JPanel {

	private static final int LINES = 8;

	private JTextPane txa_messages;
	ArrayList<String> messages = new ArrayList<String>();
	StyledDocument doc = null;

	/**
	 * Constructor
	 */
	public InformationPanel() {
		initComponents();
	}

	/**
	 * Initialization
	 */
	private void initComponents() {

		txa_messages = new javax.swing.JTextPane();
		doc = txa_messages.getStyledDocument();
		this.setForeground(java.awt.Color.black);
		this.setOpaque(false);
		Dimension d = new java.awt.Dimension(500, 300);
		this.setMaximumSize(d);
		this.setMinimumSize(d);
		this.setPreferredSize(d);
		this.setSize(d);

		MutableAttributeSet standard = new SimpleAttributeSet();
		StyleConstants.setAlignment(standard, StyleConstants.ALIGN_CENTER);
		doc.setParagraphAttributes(0, 0, standard, true);

		txa_messages.setMaximumSize(d);
		txa_messages.setMinimumSize(d);
		txa_messages.setPreferredSize(d);
		txa_messages.setOpaque(false);
		txa_messages.setEditable(false);

		SpringLayout layout = new SpringLayout();
		this.add(txa_messages);
		layout.putConstraint(SpringLayout.VERTICAL_CENTER, txa_messages, 0,
				SpringLayout.VERTICAL_CENTER, this);
		layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, txa_messages, 0,
				SpringLayout.HORIZONTAL_CENTER, this);
		this.setLayout(layout);
		txa_messages.revalidate();
		txa_messages.repaint();

		Runnable r = new Runnable() {

			public void run() {
				revalidate();
				repaint();
			}
		};
		SwingUtilities.invokeLater(r);
	}

	/**
	 * Replace the existing text with the new string
	 * 
	 * @param message
	 */
	public void setMessage(String message) {
		this.messages.clear();
		this.messages.add(message);
		txa_messages.setText(this.messages.get(0));
		revalidate();
		repaint();
	}

	/**
	 * Appends the message to the previously present text
	 * 
	 * @param message
	 */
	public void addMessage(String message) {
		this.messages.add(message);
		int size = this.messages.size();
		if (size > LINES) {
			txa_messages.setText("");
			for (int i = (size - LINES); i < this.messages.size(); i++) {
				txa_messages.setText(txa_messages.getText()
						+ ConfigurationConstGraphics.NEWLINE + "\n"
						+ messages.get(i));
			}
		} else {

			txa_messages.setText(txa_messages.getText()
					+ ConfigurationConstGraphics.NEWLINE + "\n" + message);
		}

		revalidate();
		repaint();
	}

}
